import { Component, OnInit, Input, OnDestroy } from "@angular/core";
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { Subscription } from "rxjs";
import { AlertController } from '@ionic/angular';
import { MessageService } from 'src/app/shared/service/message.service';
import { HttpService } from 'src/app/shared/service/http.service';
@Component({
  selector: 'app-opportunity-list',
  templateUrl: './opportunity-list.component.html',
  styleUrls: ['./opportunity-list.component.scss'],
})
export class OpportunityListComponent implements OnInit, OnDestroy {
  @Input() tagData: any;
  pageNo: any = 1;
  params: any;
  sessionId = localStorage.getItem("sessionId");
  // 声明订阅对象
  subscription: Subscription = null;
  constructor(
    public alertController: AlertController,
    private message: MessageService,
    private httpService: HttpService,
    private appGlobalService: AppGlobalService) { }

  ngOnInit() {
    // this.tagData = this.getDemoInputData();
    this.params = this.tagData.data.submit;
    if (!this.subscription && this.tagData.data.submit !== undefined) {
      this.message.clearMessage();
      // 获取筛选参数
      this.subscription = this.message.getMessage().subscribe(param => {
        this.pageNo = 1;
        this.params.sessionId = localStorage.getItem("sessionId");
        if (param === undefined) {
          return;
        }
        // 参数
        this.params.content = param;
        // 页面数据参数
        this.params.content.pageNo = this.pageNo;
        // 请求获取新数据
        this.httpService.post(
          "application/submit",
          this.params,
          res => {
            // console.log("submit");
            // 请求数据成功
            if (res.success) {
              const result = res.data.data;
              // 筛选数据
              this.tagData.data.data = result;
              // 自动回到顶部
              setTimeout(() => {
                const el = document.getElementById('information0');
                if (el) {
                  el.scrollIntoView(true);
                }
              }, 200);
            } else {
              // 请求数据失败
              this.appGlobalService.showToast(res.message, 2000, "middle");
            }
          },
          err => {
            // 请求数据失败
            this.appGlobalService.showToast(err.message, 2000, "middle");
          }
        );
      });
    }
  }
  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }

  getDemoInputData() {
    return {
      type: "tag",
      style: "auditsuccessListTag",
      name: "auditsuccessListTag",
      linearLayout: "body",
      data: {
        data: [
          {
            thirdRowLeft: {
              firstText: {
                label: "转让 | 张先生 18378082631"
              }
            },
            fourthRowRight: {
              firstText: {
                label: "2020-07-17 15:13:32"
              }
            },
            link: {
              type: "page",
              params: {
                opportunity_id: "5f116eeca6a57e0001653557",
                cuttab: "0",
                customer_id: "5f116ee9a6a57e0001653556"
              },
              target: "158498"
            },
            headRowRight: {
              firstText: {
                // tslint:disable-next-line:max-line-length
                cssStyle: '{"color":"rgba(72,191,48,1)"}',
                label: "审核通过"
              }
            },
            fourthRowLeft: {
              firstText: {
                // tslint:disable-next-line:max-line-length
                cssStyle: "padding: 0.19rem 0.31rem;margin-left: 0.63rem;background: linear-gradient(270deg,rgba(247,72,37,1) 0%,rgba(255,128,0,1) 100%);border-radius: 0.13rem;font-size: 0.69rem;color: #fff;line-height: 0.69rem;display: inline-block",
                label: "200元"
              }
            },
            headRowLeft: {
              firstText: {
                label: "深圳乔晓萌推荐 2020-07-17 15:13:32"
              },
              icon: {
                iconName: "xxx.png"
              }
            },
            secondRowLeft: {
              firstText: {
                label: "出租 | 张先生 18378082631"
              }
            }
          },
          {
            thirdRowLeft: {
              firstText: {
                label: "转让 | 张先生 18378082631"
              }
            },
            fourthRowRight: {
              firstText: {
                label: "2020-07-17 15:13:32"
              }
            },
            link: {
              type: "page",
              params: {
                opportunity_id: "5ea54430c162490001d4af38",
                cuttab: "0",
                customer_id: "5d3531d9c9517b00013a3adc"
              },
              target: "158498"
            },
            headRowRight: {
              firstText: {
                // tslint:disable-next-line:max-line-length
                cssStyle: "padding: 0.19rem 0.31rem;margin-left: 0.63rem;background: linear-gradient(270deg,rgba(247,72,37,1) 0%,rgba(255,128,0,1) 100%);border-radius: 0.13rem;font-size: 0.69rem;color: #fff;line-height: 0.69rem;display: inline-block",
                label: "待审核"
              }
            },
            fourthRowLeft: {
              firstText: {
                // tslint:disable-next-line:max-line-length
                cssStyle: "padding: 0.19rem 0.31rem;margin-left: 0.63rem;background: linear-gradient(270deg,rgba(247,72,37,1) 0%,rgba(255,128,0,1) 100%);border-radius: 0.13rem;font-size: 0.69rem;color: #fff;line-height: 0.69rem;display: inline-block",
                label: "200元"
              }
            },
            headRowLeft: {
              firstText: {
                label: "深圳乔晓萌推荐 2020-07-17 15:13:32"
              },
              icon: {
                iconName: "xxx.png"
              }
            },
            secondRowLeft: {
              firstText: {
                label: "出租 | 张先生 18378082631"
              }
            }
          }
        ],
        submit: {
          content: {
            showName: "#opportunity_transfer",
            pageNo: 1
          },
          id: "0fbe3808-aa94-41c2-9883-c15ea61cb45c"
        }
      },
      pageSize: 2,
      pageNo: 1,
      pageTotal: 346
    };
  }

  /***
   * 脚本配置方法
   * {&quot;margin-bottom&quot;:&quot;1.93rem&quot;}
   */
  getNgStyle(str) {
    try {
      return JSON.parse(str);
    } catch (error) {
      return {};
    }
  }
  loadData(event) {
    setTimeout(() => {
      this.params.sessionId = localStorage.getItem("sessionId");
      // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      this.params.content.pageNo = this.pageNo;
      // 请求获取新数据
      this.httpService.post("application/submit", this.params, res => {
        // 请求数据成功
        if (res.success) {
          // 追加返回数据
          if (Array.isArray(res.data.data) && res.data.data.length > 0) {
            // 追加数据
            res.data.data.map(item => {
              this.tagData.data.data.push(item);
            });
            // this.params = res.data.submit;
          } else {
            // 没有数据了
            // this.appGlobalService.showToast("没有数据了...", 1000, "bottom");
          }
          event.target.complete();
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, "middle");
          event.target.complete();
        }
      });
    }, 50);
  }

  go(item) {
    const params = {
      pageId: item.link.target,
      params: item.link.params,
      sessionId: localStorage.getItem("sessionId")
    };
    this.appGlobalService.goToForward(params);
  }

  // 打电话
  async call(e) {
    event.stopPropagation();
    const alert = await this.alertController.create({
      header: '',
      subHeader: '',
      message: e,
      buttons: [{
        text: '取消',
        role: 'cancel',
        cssClass: 'secondary',
        handler: () => {
          console.log('取消');
        }
      }, {
        text: '拨打',
        cssClass: 'LJ',
        handler: () => {
          this.httpService.changeCallNumber(e);
        }
      }]
    });
    await alert.present();
  }
}
